package net.oschina.app.myapp;

import android.app.Application;
import android.content.Context;
import android.os.Handler;

import net.oschina.app.interf.Constants;
import net.oschina.app.util.CommonUtils;

import java.io.File;
import java.io.IOException;

import okhttp3.Cache;
import okhttp3.CacheControl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.converter.simplexml.SimpleXmlConverterFactory;

/**
 * Created by wei03 on 2017/1/12.
 */

public class MyApplication extends Application {
    private static Context context;
    private static Thread mainThread;
    private static int mainThreadId;
    private static Handler mHandler;
    public static Retrofit mRetrofit;
    private OkHttpClient mClient;
    @Override
    public void onCreate() {
        context = this.getApplicationContext();
        mainThread = Thread.currentThread();
        mHandler=new Handler();
        mainThreadId=android.os.Process.myTid();//取得线程id，主线程id
        initOKHttp();
        initRetrofit();
    }
    public static Context getContext() {
        return context;
    }
    public static Thread getMainThread() {
        return mainThread;
    }
    public static Handler getHandler() {
        return mHandler;
    }

    public static int getMainThreadId() {
        return mainThreadId;
    }
    /** 初始化OKHttp实现缓存 */
    private void initOKHttp() {
        File cacheDir = new File(getCacheDir(), "responses");
        mClient = new OkHttpClient
                .Builder()
                .cache(new Cache(cacheDir, 10 * 1024 * 1024))
                .addInterceptor(mInterceptor)
                .build();

    }

    /** 初始化Retrofit */
    private void initRetrofit() {
        mRetrofit = new Retrofit.Builder()
                .baseUrl(Constants.HOST)
                .addConverterFactory(SimpleXmlConverterFactory.create())
                .client(mClient)
                .build();
    }

    /** 缓存拦截器 */
    Interceptor mInterceptor = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            if (!CommonUtils.isNetworkReachable(getApplicationContext())) {
                request = request.newBuilder()
                        .cacheControl(CacheControl.FORCE_CACHE)
                        .build();
            }

            Response response = chain.proceed(request);
            if (CommonUtils.isNetworkReachable(getApplicationContext())) {
                int maxAge = 60 * 60; // read from cache for 1 minute
                response.newBuilder()
                        .removeHeader("Pragma")
                        .header("Cache-Control", "public, max-age=" + maxAge)
                        .build();
                System.out.println("aaaaa");
            } else {
                int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
                response.newBuilder()
                        .removeHeader("Pragma")
                        .header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
                        .build();
                System.out.println("bbbb");
            }
            return response;
        }
    };
}
